package mysql

import "gorm.io/gorm"

type Order struct {
	gorm.Model
	UserId      int64   `gorm:"type:int;not null;" json:"userId"`
	OrderSn     string  `gorm:"type:char(36);not null;unique;" json:"orderSn"`
	ScenicId    int64   `gorm:"type:int;not null;" json:"scenicId"`
	AdmissionId int64   `gorm:"type:int;not null;" json:"admissionId"`
	Num         int64   `gorm:"type:int;not null;" json:"num"`
	Price       float64 `gorm:"decimal(10,2);not null;" json:"price"`
	Status      int64   `gorm:"type:int;" json:"status"`
	InsureId    int64   `gorm:"type:int;" json:"insureId"`
}
type UserIds struct {
	OrderSn int64 `gorm:"type:int;not null;" json:"orderSn"`
	UserId  int64 `gorm:"type:int;not null;" json:"userId"`
}

func (o *Order) FindOrderById(id int64) (order []*Order, err error) {
	err = DB.Where("user_id=?", id).Find(&order).Error
	if err != nil {
		return nil, err
	}
	return order, nil

}
func (o *Order) UpdatePrice(price float64) bool {
	err := DB.Model(&o).Update("price", price).Error
	if err != nil {
		return false
	}
	return true
}

func (o *Order) CreateOrder() error {
	err := DB.Create(&o).Error
	if err != nil {
		return err
	}
	return nil
}
func (us *UserIds) GetUserIds(orderSn string) (*UserIds, error) {
	err := DB.Where("order_sn=?", orderSn).Find(&us).Error
	if err != nil {
		return nil, err
	}
	return us, nil
}
func (o *Order) UpdateOrderStatus(OrderSn string, Status int) error {
	err := DB.Model(&o).Where("order_sn=?", OrderSn).Update("status", Status).Error
	if err != nil {
		return err
	}
	return nil
}
func (o *Order) GetStatus(orderSn string) (*Order, error) {
	err := DB.Where("order_sn=?", orderSn).Find(&o).Error
	if err != nil {
		return nil, err
	}
	return o, nil
}
